package com.ramotion.expandingcollection;

import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

import java.util.List;

/**
 * AlphaScalePageTransformer
 *
 * @since 2021-05-31
 */
public final class AlphaScalePageTransformer {
    /**
     * 缩放
     */
    public static final float INACTIVE_SCALE = 0.8f;
    /**
     * 透明度
     */
    public static final float INACTIVE_ALPHA = 0.5f;

    /**
     * AlphaScalePageTransformer
     */
    private AlphaScalePageTransformer() {
    }

    /**
     * defaultPage
     *
     * @param child
     * @param position
     * @param current
     */
    public static void defaultPage(ListContainer child, int position, float current) {
        if (position != current) {
            child.setAlpha(INACTIVE_ALPHA);
            child.setScaleX(INACTIVE_SCALE);
            child.setScaleY(INACTIVE_SCALE);
        }
    }

    /**
     * defaultPage
     *
     * @param childList
     * @param position
     */
    public static void endPage(List<ListContainer> childList, int position) {
        if (position >= 1) {
            ListContainer child = childList.get(position - 1);
            child.setAlpha(INACTIVE_ALPHA);
            child.setScaleX(INACTIVE_SCALE);
            child.setScaleY(INACTIVE_SCALE);
        }
    }

    /**
     * transformPage
     *
     * @param childList
     * @param position
     * @param offset
     * @param direction
     */
    public static void transformPage(List<ListContainer> childList, int position, float offset, float direction) {
        Component child = childList.get(position);
        float scale = INACTIVE_SCALE + (1 - INACTIVE_SCALE) * (1 - Math.abs(offset));
        float alpha = INACTIVE_ALPHA + (1 - INACTIVE_ALPHA) * (1 - Math.abs(offset));
        child.setScaleX(scale);
        child.setScaleY(scale);
        child.setAlpha(alpha);
        if (direction > 0) {
            if (position < childList.size() - 1) {
                child = childList.get(position + 1);
            }
        } else {
            if (position >= 1) {
                child = childList.get(position - 1);
            }
        }
        scale = INACTIVE_SCALE + (1 - INACTIVE_SCALE) * Math.abs(offset);
        alpha = INACTIVE_ALPHA + (1 - INACTIVE_ALPHA) * Math.abs(offset);
        child.setScaleX(scale);
        child.setScaleY(scale);
        child.setAlpha(alpha);
    }
}
